package com.linln.admin.meeting.repository;

import com.linln.admin.meeting.domain.OrderInfo;
import com.linln.modules.system.repository.BaseRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

public interface OrderRepository extends BaseRepository<OrderInfo, Long> {

    @Query(nativeQuery = true, value = "SELECT o.* FROM `meeting_order_info` o,`meeting_room` m,`meeting_group` g WHERE o.`meeting_id`=m.`id` AND m.`group_id`=g.`id`" +
            " AND g.`id`=:groupId AND o.`start_time`>=:startTime AND o.`end_time`<=:endTime")
    List<OrderInfo> selectOrdersByRange(@Param("groupId") Long groupId, @Param("startTime") Date startTime, @Param("endTime") Date endTime);

    /**
     * 选定的时间范围有没有被使用掉
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Query(value = "SELECT count(*) FROM OrderInfo  WHERE meetingId=:roomId and ((startTime<=:start AND endTime>=:start) or (startTime<=:end AND endTime>=:end))")
    int existsByTimeRange(@Param("roomId") Long roomId,@Param("start") Date startTime, @Param("end") Date endTime);
}
